<?php

namespace App\Exceptions;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Throwable;
use Rainsens\Dinglog\Facades\Dinglog;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
	    TokenExpiredException::class,
        TokenBlacklistedException::class,
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

	/**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    public function report(Throwable $e)
    {
    	if ($e instanceof ValidationException) {
		    parent::report($e);
	    } elseif ($e instanceof MethodNotAllowedHttpException) {
            parent::report($e);
        } elseif ($e->getMessage() == 'Unauthenticated.') {
		    parent::report($e);
	    } elseif ($e->getMessage() == 'Token has expired') {
		    parent::report($e);
	    } elseif ($e->getMessage() == 'Token has expired and can no longer be refreshed') {
		    parent::report($e);
	    } elseif (Str::startsWith($e->getMessage(), 'No query results for model')) {
		    parent::report($e);
	    } elseif ($e->getMessage() == 'CSRF token mismatch.') {
    		parent::report($e);
	    } elseif (empty($e->getMessage())) {
		    parent::report($e);
        } elseif ($e->getMessage() == 'Route [login] not defined.') {
            return Unauthenticated('Unauthenticated.');
	    } else {
		    /*Dinglog::exception(
			    $e->getMessage(),
			    $e->getFile(),
			    $e->getCode(),
			    $e->getLine(),
			    json_encode($e->getTrace())
		    );*/
		    parent::report($e);
	    }
    }
}
